#include "usb_mdwr.h"

#include "usb_lib.h"
#include "hw_config.h"
#include "usb_pwr.h"


/**
 * Dummy prototype.
 */
static void DummyEpOut(uint8_t*, uint8_t);
static DataForEpInHandler DummyEpIn(void);

static const uint8_t bMessage[] = "This is dummy function";

EpOutIrqHandler_t USB_EpOutIrqHandlers[MDWR_NUM_OUT_EP] =
{
	DummyEpOut,	/* EP2_OUT_Callback */
	DummyEpOut	/* EP4_OUT_Callback */

};

EpInIrqHandler_t USB_EpInIrqHandlers[MDWR_NUM_IN_EP] =
{
	DummyEpIn, /* EP1_IN_Callback */
	DummyEpIn  /* EP3_IN_Callback */
};

/**
 * Initialized at the level of middleware.
 */
ErrorStatus USB_MDWR_Init(void)
{
	USB_Pullup MDWR_USB_Pullup;

	/* Configuration Pullup pin. */
	MDWR_USB_Pullup.USB_GPIO_Mode = GPIO_Mode_Out_PP;
	MDWR_USB_Pullup.USB_GPIO_Pin = GPIO_Pin_2;
	MDWR_USB_Pullup.USB_GPIO_Speed = GPIO_Speed_50MHz;
	MDWR_USB_Pullup.USB_GPIO_State = ENABLE;
	MDWR_USB_Pullup.USB_GPIOx = GPIOD;
	MDWR_USB_Pullup.USB_RCC_APB2Periph = RCC_APB2Periph_GPIOD;

 	 /* USB configuration. */
	USB_Disconnect_Config(&MDWR_USB_Pullup);
	Set_USBClock();
    USB_Interrupts_Config();
    USB_Init();

    /* USB startup done. */
    while (bDeviceState != CONFIGURED); /* Times should be set exit. */

    return SUCCESS;
}

/*
 *  Enable the transmit of data on MDWR_NUM_IN_EP_0.
 */
void USB_MDWR_SetEp0TxValid(void)
{
	/* Enable the transmit of data on EP1 */
    SetEPTxValid(ENDP1);
}

/*
 *  Enable the transmit of data on MDWR_NUM_IN_EP_1.
 */
void USB_MDWR_SetEp1TxValid(void)
{
	/* Enable the transmit of data on EP3 */
	SetEPTxValid(ENDP3);
}

/**
 *  Nop process.
 */
static void DummyEpOut(uint8_t* pBufferPointer, uint8_t wBufferSize)
{
}

/**
 *  Send message: 'This is dummy function'.
 */
static DataForEpInHandler DummyEpIn(void)
{
	DataForEpInHandler dummy;

	dummy.pBufferPointer = (uint8_t *)bMessage;
	dummy.wBufferSize = sizeof(bMessage);
	return dummy;
}

